﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ascx_modify : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
   //superType列表框触发subType列表框内容改变，并将其选定项显示在superType文本框中
    protected void LB_superType_SelectedIndexChanged(object sender, EventArgs e)
    {
        LB_book.Items.Clear();
        LB_subType.Items.Clear();
        SubType subs = new SubType();
        int id =Convert.ToInt32(LB_superType.SelectedValue);
        
        foreach (var sub in subs.GetSubTypeNameBySuperId(id))
        {
            LB_subType.Items.Add(sub.subTypeName);
            LB_subType.Items.FindByText(sub.subTypeName).Value = Convert.ToString(sub.subTypeId);

        }
        TB_SuperType.Text = LB_superType.SelectedItem.Text;
        TB_sub_superType.Text = LB_superType.SelectedItem.Text;
    }

    //subType列表框触发book列表框内容改变，并将其选定项显示在下面两个只读文本框中
    protected void LB_subType_SelectedIndexChanged(object sender, EventArgs e)
    {
        LB_book.Items.Clear();
        BookInfo books = new BookInfo();
        int id = Convert.ToInt32(LB_subType.SelectedValue);
        foreach (var book in books.GetBookBySubType(id))
        {
            LB_book.Items.Add(book.bookName);
            LB_book.Items.FindByText(book.bookName).Value = Convert.ToString(book.bookId);
        }
        TB_subType.Text = LB_subType.SelectedItem.Text;
        
    }
    //book列表框选定项绑定datalis
    protected void LB_book_SelectedIndexChanged(object sender, EventArgs e)
    {
        BookInfo books = new BookInfo();
        int id = Convert.ToInt32(LB_book.SelectedValue);
        DataList1.DataSource = books.GetBookNameById(id);
        DataList1.DataBind();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    //删除大分类
    protected void BTN_deleteSuperType_Click(object sender, EventArgs e)
    {
        if (LB_superType.SelectedIndex >= 0)//判断是superType否有选中项
        {
            SuperType supers = new SuperType();
            int id = Convert.ToInt32(LB_superType.SelectedValue);
            supers.DeleteSuperType(id);
            LB_superType.Items.Remove(LB_superType.SelectedItem);
            LB_subType.Items.Clear();
            LB_book.Items.Clear();
        }

    }
    //删除小分类
    protected void BTN_deleteSupType_Click(object sender, EventArgs e)
    {
        if (LB_subType.SelectedIndex >= 0)//判断subType是否有选中项
        {
            SubType subs = new SubType();
            int id = Convert.ToInt32(LB_subType.SelectedValue);
            subs.DeleteSubType(id);
            LB_subType.Items.Remove(LB_subType.SelectedItem);
            LB_book.Items.Clear();
        }
    }
    //删除图书
    protected void BTN_deleteBook_Click(object sender, EventArgs e)
    {
        if (LB_book.SelectedIndex >= 0)//判断book是否有选中项
        {
            BookInfo books = new BookInfo();
            int id = Convert.ToInt32(LB_book.SelectedValue);
            books.deleteBook(id);
            LB_book.Items.Remove(LB_book.SelectedItem);
        }
    }
    //增加大分类
    protected void BTN_increaseSuperType_Click(object sender, EventArgs e)
    {
        SuperType supers = new SuperType();
        int id=supers.InsertSuperTypeReturnId(TB_superIncrease.Text);
        LB_superType.Items.Add(TB_superIncrease.Text);
        LB_superType.Items.FindByText(TB_superIncrease.Text).Value = Convert.ToString(id);

        TB_superIncrease.Text = null;
    }
    //增加小分类
    protected void BTN_increaseSubType_Click(object sender, EventArgs e)
    {
        SubType subs = new SubType();
        int id = subs.InsertSubTypeReturnId(TB_subIncrease.Text,Convert.ToInt32(LB_superType.SelectedValue));
        LB_subType.Items.Add(TB_subIncrease.Text);
        LB_subType.Items.FindByText(TB_subIncrease.Text).Value = Convert.ToString(id);

        TB_subIncrease.Text = null;
        TB_sub_superType.Text = null;
    }

    //增加图书
    protected void BTN_book_Click(object sender, EventArgs e)
    {

        //上传图片
        string path;
        string mapRootPath = "~/images/book/";  //mapRootPath可以在配置文件中设置，图片保存的根路径
        if (FileUpload1.HasFile)
        {
            string uploadPath = Server.MapPath(mapRootPath);
            path = mapRootPath + FileUpload1.FileName;
            FileUpload1.SaveAs(uploadPath + FileUpload1.FileName);


            //将数据插入数据库
            BookInfo books = new BookInfo();
            long id = books.InsertBookReturnId(TB_name.Text, Convert.ToString(LB_subType.SelectedValue),
                                                TB_author.Text, TB_publicher.Text, TB_ISBN.Text, TB_marketPrice.Text,
                                                TB_inventory.Text, TB_introduce.Text, path, null);
            LB_book.Items.Add(TB_name.Text);
            LB_book.Items.FindByText(TB_name.Text).Value = Convert.ToString(id);

            TB_name.Text = null;
            TB_publicher.Text = null;
            TB_marketPrice.Text = null;
            TB_ISBN.Text = null;
            TB_inventory.Text = null;
            TB_author.Text = null;
            TB_introduce.Text = null;
        }
    }
    //设定公共折扣率
    protected void DL_publicDiscount_SelectedIndexChanged(object sender, EventArgs e)
    {
        BookInfo books = new BookInfo();
        books.updatePublicDiscount(Convert.ToInt16(DL_publicDiscount.SelectedValue));
    }

    //选择是否开启折扣
    protected void CB_isPublicDiscount_CheckedChanged(object sender, EventArgs e)
    {
        BookInfo books = new BookInfo();
        if (CB_isPublicDiscount.Checked)
        {
            books.enabledPublicDiscount(true);
        }
        else
        {
            books.enabledPublicDiscount(false);
        }
    }
    //点击编辑图书按钮，在文本框中显示图示信息，再删除图书
    protected void BTN_modefiy_Click(object sender, EventArgs e)
    {
        BookInfo books = new BookInfo();
        long id = Convert.ToInt64(LB_book.SelectedValue);
        foreach(var book in books.GetBookNameById(id))
        {
            TB_name.Text = book.bookName;
            TB_publicher.Text = book.publisher;
            TB_marketPrice.Text =Convert.ToString(book.marketPrice);
            TB_ISBN.Text = book.ISBN;
            TB_inventory.Text = Convert.ToString(book.inventory);
            TB_author.Text = book.author;
            TB_introduce.Text = book.introduce;
        }

        books.deleteBook(id);
        LB_book.Items.Remove(LB_book.SelectedItem);
    }
    
}